package com.hxzy.common.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2 {

    //    http://localhost:8088/swagger-ui.html     原路径
    //    http://localhost:8088/doc.html            新路径
    /**
     * 全局参数
     *
     * @return
     */

    private List<Parameter> parameter() {
        List<Parameter> params = new ArrayList<Parameter>();
        params.add(new ParameterBuilder().name("token")
                .description("认证信息：token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build());
        return params;
    }



    @Bean
    public Docket createRestApi() {
        Predicate<RequestHandler> adminPredicate = RequestHandlerSelectors.basePackage("com.hxzy.controller");

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                //需要扫描的路劲
                .apis(RequestHandlerSelectors.basePackage("com.hxzy.controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(parameter());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("华信智原·教务管理系统api")                       // 文档页标题
                .contact(new Contact("hxzy",
                        "https://www.hxzy.com",
                        "abc@hxzy.com"))                   // 联系人信息
                .description("专为华信智原新闻·教务管理系统提供的api文档")      // 详细信息
                .version("1")                               // 文档版本号
                .termsOfServiceUrl("https://www.hxzy.com")     // 网站地址
                .build();
    }
}